﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Lephone.Data;
using InfoManager;

namespace SimlvClient.Pages
{
    public class CreatPage
    {
        public void UpdatePage()
        {
            var ModelList = DbEntry.From<SimlvClient.Model.StuOperPageConfig>().Where(Lephone.Data.Condition.Empty).OrderByDescending(S => S.Id).Select().ToList();
            //List<string> HasProcPage = new List<string>();
            //var WebSiteModel = DbEntry.From<SimlvClient.Model.WebSites>().Where(Lephone.Data.Condition.Empty).Select().SingleOrDefault();

            foreach (var Model in ModelList)
            {
                try
                {
                    //IsSimple and Index
                    if (Model.IsSimpleId == 1 && Model.CateId == 0)
                    {

                        string Url = Directory.GetCurrentDirectory() + "\\" + "templete" + "\\" + Model.TempUrl;
                        FileInfo fileInf = new FileInfo(Url);
                        using (Stream fileStream = fileInf.OpenRead())
                        {
                            StreamReader reader = new StreamReader(fileStream, Encoding.GetEncoding("gb2312"));
                            String row = null;
                            StringBuilder sb = new StringBuilder();
                            do
                            {
                                row = reader.ReadLine();
                                sb.Append(row);
                                sb.Append(Environment.NewLine);
                            } while (row != null);
                            var ContentModelList = DbEntry.From<SimlvClient.Model.StuBookChapter>().Where(o => o.BookId == 10).Select().ToList();
                            foreach (var ContentModel in ContentModelList)
                            {
                                int WordsLength = ContentModel.ChapterContent.Length;
                                string WordsContent = ContentModel.ChapterContent;
                             
                                char expectNextChar = '\r';
                                char expectNChar = '\n';
                                int PageSize = 1000000000;
                                var Sentence = ContentModel.ChapterContent.Split('。');
                                if (Sentence.Length > PageSize)
                                {

                                    int TotalPage = Sentence.Length / PageSize;
                                    bool IsLastPage = (Sentence.Length % PageSize > 0);
                                    if (IsLastPage)
                                    {
                                        TotalPage = TotalPage + 1;
                                    }
                                    string Pagelength = string.Empty;
                                    for (int i = 0; i < TotalPage; i++)
                                    {
                                        string SubContent = string.Empty;
                                        string LastContent = string.Empty;
                                        if (i != TotalPage - 1)
                                        {
                                            for (int j = i * PageSize; j < (i + 1) * PageSize; j++)
                                            {
                                                if (Sentence[j].Trim() != "")
                                                {
                                                    SubContent = SubContent + Sentence[j] + "。";
                                                }
                                            }
                                            LastContent = ReplacePageTag(SubContent.Length, SubContent, LastContent, expectNextChar, expectNChar);
                                        }
                                        else
                                        {
                                            for (int j = i * PageSize; j < Sentence.Length; j++)
                                            {
                                                if (Sentence[j].Trim() != "")
                                                {
                                                    SubContent = SubContent + Sentence[j] + "。";
                                                }
                                            }
                                            LastContent = ReplacePageTag(SubContent.Length, SubContent, LastContent, expectNextChar, expectNChar);
                                        }
                                        var Newrow = sb.ToString().Replace("{content}", LastContent);

                                        StringBuilder sb1 = new StringBuilder();
                                        sb1.Append("<div class=\"badoo\">");
                                        if (i == 0)
                                        {
                                            sb1.Append("<span class=\"disabled\"> <  向前</span>");
                                        }
                                        else
                                        {
                                            sb1.Append("<a href=\"" + Model.TempUrl.Replace(".html", ContentModel.Id.ToString() + "_" + i.ToString() + ".html") + "\"> <  向前</a>");
                                        }
                                        for (int s = 1; s <= TotalPage; s++)
                                        {

                                            if (s == (i+1))
                                            {
                                                sb1.Append("<span class=\"current\">" + s.ToString() + "</span>");
                                            }
                                            else
                                            {
                                                sb1.Append("<a href=\"" + Model.TempUrl.Replace(".html", ContentModel.Id.ToString() + "_" + s.ToString() + ".html") + "\">" + s.ToString() + "</a>");
                                            }
                                        }
                                        if ((i+1) == TotalPage)
                                        {
                                            sb1.Append("<span class=\"disabled\">向后  > </span>");
                                        }
                                        else
                                        {
                                            sb1.Append("<a href=\"" + Model.TempUrl.Replace(".html", ContentModel.Id.ToString() + "_" + (i + 2).ToString() + ".html") + "\">向后  > </a></div>");
                                        }

                                        FileHelper.Write(Newrow.Replace("{page}", sb1.ToString()), @"k:\Project\MyProject\SimlvClient\SimlvClient\test\" + Model.TempUrl.Replace(".html", ContentModel.Id.ToString() + "_" + (i+1).ToString() + ".html"));
                                    }

                                }
                                else
                                {
                                    string LastContent = string.Empty;
                                    LastContent = ReplacePageTag(WordsLength ,WordsContent, LastContent, expectNextChar, expectNChar);
                                    var Newrow = sb.ToString().Replace("{content}", LastContent);
                                    // {page}
                                    //<div class="badoo"><span class="disabled"> <  向前</span><span class="current">1</span><a href="#">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">6</a><a href="#">7</a>...<a href="#">199</a><a href="#">200</a><a href="#">向后  > </a></div>
                                    FileHelper.Write(Newrow, @"K:\Project\MyProject\SimlvClient\SimlvClient\test\" + Model.TempUrl.Replace(".html", ContentModel.Id.ToString() + ".html"));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }

        }

        private static string ReplacePageTag(int WordsLength, string WordsContent, string LastContent, char expectNextChar, char expectNChar)
        {
            for (int i = 0; i < WordsLength; i++)
            {
                string SubTemp = WordsContent.Substring(i, 1);

                if (SubTemp != "" && CompareInfo.isHanZi(char.Parse(SubTemp)))
                {
                    LastContent = LastContent + "<A class=fontbox >" + SubTemp + "<SPAN>" + PinYin.ToPinYin(SubTemp) + "</SPAN></A>";
                }
                else if (char.Parse(SubTemp) == expectNextChar || char.Parse(SubTemp) == expectNChar)
                {
                    LastContent = LastContent + "</br>";
                }
                else if (SubTemp.Trim() == "")
                {
                    LastContent = LastContent + "&nbsp;&nbsp;&nbsp;";
                }

                else
                {
                    LastContent = LastContent + "<A class=fontbox >" + SubTemp + "<SPAN>&nbsp;</SPAN></A>";
                }
            }
            return LastContent;
        }
    }


}
